<template>
  <div class="register-container">
    <el-card class="register-box" shadow="hover">
      <h3 style="text-align: center; margin-bottom: 20px;">求职者注册</h3>
      <el-form :model="form" :rules="rules" ref="registerForm" label-width="80px" size="small">
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="form.name" placeholder="请输入姓名"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="年龄" prop="age">
              <el-input-number v-model="form.age" :min="16" :max="80" style="width: 100%"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="身份证" prop="identity">
          <el-input v-model="form.identity" placeholder="请输入身份证号"></el-input>
        </el-form-item>

        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号"></el-input>
        </el-form-item>

        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱"></el-input>
        </el-form-item>

        <el-form-item label="所在地" prop="location">
          <el-input v-model="form.location" placeholder="如：北京市海淀区"></el-input>
        </el-form-item>

        <el-form-item label="账号" prop="account">
          <el-input v-model="form.account" placeholder="设置登录账号"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="form.password" placeholder="设置登录密码"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submit" style="width: 100%">注 册</el-button>
        </el-form-item>

        <div style="text-align: center;">
          <span>已有账号？</span>
          <el-button type="text" @click="$router.push('/user_login')">去登录</el-button>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import request from '@/utils/request';

export default {
  name: 'UserRegister',
  data() {
    return {
      form: {
        name: '',
        age: null,
        identity: '',
        phone: '',
        email: '',
        location: '',
        resume: '',
        attachments: '',
        account: '',
        password: '',
        desiredJobId: null
      },
      rules: {
        name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        account: [{ required: true, message: '请输入账号', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { type: 'email', message: '邮箱格式不正确', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    submit() {
      this.$refs.registerForm.validate(valid => {
        if (!valid) return;

        request.post('/User_Register', this.form)
          .then(res => {
            const data = res.data;
            if (data.code === 1) {
              this.$message.success('注册成功，请登录！');
              this.$router.push('/user_login');
            } else {
              this.$message.error(data.msg || '注册失败');
            }
          })
          .catch(err => {
            this.$message.error('网络错误，请检查后端是否运行');
            console.error(err);
          });
      });
    }
  },
  created() {
    // 如果已登录，跳转主页
    // if (localStorage.getItem('user_token')) {
    //   this.$router.push('/user_main_page');
    // }
  }
};
</script>

<style scoped>
.register-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}

.register-box {
  width: 500px;
  padding: 30px;
  border-radius: 10px;
}
</style>